package a66weding.com.jiehuntong.activity.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.MyACAdapter;
import a66weding.com.jiehuntong.beans.CommonResultBean;
import a66weding.com.jiehuntong.beans.DiyaddBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MarriageProposalBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;

/**
 * Created by admin on 2016/9/22.
 * 流程增加或者删除
 */
public class LiuChengActivity extends Activity implements OnNetResultListener,AdapterView.OnItemClickListener,View.OnClickListener{

    public int flag;//1为增加流程 2为删除流程;
    //删除
    private Button add_btn;
    //适配器;
    private MyACAdapter mAdapter;
    //listView
    private ListView lv;
    private String[] strName;
    //计数的
    private int checkNum;
    //全选的iamge
    private ImageView item_add_iv;
    //全选容器
    private RelativeLayout add_groop;
    //被删除或者是被增添的数据
    private List<String> data;
    //如果为true的时候全选;
    private boolean tag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diy_listview_add);
        flag = getIntent().getIntExtra("LiuChengActivity",-1);
        strName = new String[] {"订婚", "登记", "对戒", "酒店", "婚纱照", "婚庆", "礼服", "邀请", "婚品", "化妆司仪", "摄影录像", "婚车", "典礼", "回娘家", "蜜月"};
        data = new ArrayList<>();
        list = new ArrayList<>();
        findView();
        initView(flag);
        getLiucheng();
    }
    private void  findView(){
        add_groop = (RelativeLayout) findViewById(R.id.add_groop);
        add_groop.setOnClickListener(this);
        add_btn = (Button) findViewById(R.id.add_btn);
        add_btn.setOnClickListener(this);
        lv = (ListView) findViewById(R.id.lv);
        lv.setOnItemClickListener(this);
        item_add_iv = (ImageView) findViewById(R.id.item_add_iv);
    }

    private void initView(int flag){
        if (flag ==1){
            //增加流程;
            add_btn.setText("增加流程");
        }else{
            //删除流程;
            add_btn.setText("删除流程");
        }
    }

    private MarriageProposalBean bean;
    private List<MarriageProposalBean.DataBean> dataBean;
    private List<DiyaddBean> list;//需要删除的item
    private CommonResultBean combean;
    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if (data instanceof MarriageProposalBean) {
            bean = (MarriageProposalBean) data;
            switch (bean.getResCode()) {
                case "0000":
                    dataBean = bean.getData();
                    if (dataBean.size()!=0){
                        change2Bean(dataBean,flag);
                    }
                    break;
                case "0003"://没有数据的时候
                    changStr2list(strName);
                    break;
            }
        }
        if (data instanceof CommonResultBean){
            add_btn.setEnabled(true);
            combean = (CommonResultBean) data;
            switch (combean.getResCode()){
                case "0000":
                    if (flag==1){
                        CUtils.showToast("新增流程成功！");
                    }else{
                        CUtils.showToast("删除流程成功！");
                    }
                    Intent intent = new Intent();
                    setResult(5,intent);
                    finish();
                    break;
            }
        }
    }

    private void changStr2list(String[] strName) {
        if (flag==2){
            item_add_iv.setSelected(false);
            add_groop.setEnabled(false);
            return;
        }
        checkNum = 0;
        for(int i = 0;i<strName.length;i++){
            DiyaddBean diyaddBean = new DiyaddBean();
            diyaddBean.setName(strName[i]);
            diyaddBean.setFlag(false);
            list.add(diyaddBean);
        }
        mAdapter = new MyACAdapter(list,this);
        lv.setAdapter(mAdapter);
    }

    @Override
    public void onError(ErrorBean errorBean) {
        if(errorBean.getRequestCode().equals(RequestCode.RETURN_PROCESS)){
            add_btn.setEnabled(true);
        }
    }

    /**
     * 获取流程;
     */
    private void getLiucheng(){
        String userUrl1 = UrlDealUtil.getRealUrl(RequestCode.RETURN_PROCESS);
        HttpRequestBean requestBean = new HttpRequestBean(RequestCode.POSTEQUEST, userUrl1, this, MarriageProposalBean.class, RequestCode.RETURN_PROCESS);
        HttpHandleUtil.executeCommonPost(this, requestBean);
    }
    //把流程转化为bean对象
    private void change2Bean(List<MarriageProposalBean.DataBean> dataBean,int flag){
        //如果flag为需要增加流程;
        if (flag==1){
            checkNum = 0;
            for (int j = 0 ;j<strName.length;j++){
                boolean lag = false;
                DiyaddBean diyaddBean = new DiyaddBean();
                diyaddBean.setName(strName[j]);
                for (int i = 0;i<dataBean.size();i++){
                    if (dataBean.get(i).getBookTitle().equals(strName[j])){
                        lag = true;
                        ++checkNum;
                        break;
                    }
                }
                diyaddBean.setFlag(lag);
                list.add(diyaddBean);
            }

        }else{//需要删除的流程;
            checkNum = 15-dataBean.size();
            for (int i = 0 ;i<dataBean.size();i++){
                DiyaddBean diyaddBean = new DiyaddBean();
                diyaddBean.setFlag(false);
                diyaddBean.setName(dataBean.get(i).getBookTitle());
                list.add(diyaddBean);
            }
        }
        mAdapter = new MyACAdapter(list,this);
        if (checkNum==15) {
            if (flag==1){
                item_add_iv.setSelected(true);
                add_groop.setEnabled(false);
            }
        }

        lv.setAdapter(mAdapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyACAdapter.ViewHolder holder = (MyACAdapter.ViewHolder) view.getTag();
        if (list.get(position).isFlag()==true){
            //如果被选中了
            return;
        }else{
            // 改变CheckBox的状态
            holder.cb.toggle();
            // 将CheckBox的选中状况记录下来
            MyACAdapter.getMap().put(position, holder.cb.isChecked());
            if (holder.cb.isChecked()){
                ++checkNum;
                data.add(list.get(position).getName());
            }else{
                --checkNum;
                data.remove(list.get(position).getName());
            }
            if (checkNum==15){
                //当被全选的时候;
                item_add_iv.setBackgroundResource(R.mipmap.selete);
                tag = false;
            }else{
                //没有被全选的时候;
                item_add_iv.setBackgroundResource(R.mipmap.no_selete);
                tag = true;
            }
        }
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_groop:
                if (flag==1) {//如果是增加流程
                    if (tag) {
                        item_add_iv.setBackgroundResource(R.mipmap.selete);
                        setAllItem();
                    } else {
                        item_add_iv.setBackgroundResource(R.mipmap.no_selete);
                        setCancleAll();
                    }
                }else{//如果是减少流程;
                    if (tag){
                        item_add_iv.setBackgroundResource(R.mipmap.selete);
                        setAllItem2();
                    }else{
                        item_add_iv.setBackgroundResource(R.mipmap.no_selete);
                        setCancleAll2();
                    }
                }
                break;

            //确认增加或者删除阿牛;
            case R.id.add_btn:
                add_btn.setEnabled(false);
                String result =  bianli(data);
                if (result==null){
                    if(checkNum==strName.length){
                        CUtils.showToast("流程已全部添加！");
                        finish();
                    }else {
                        CUtils.showToast("请选择流程");
                    }
                    return;
                }
                if (flag==1){
                    //增加流程
                    getNetAdd(result);
                }else{
                    //减少流程
                    cutBack(result);
                }
                break;
        }
    }
    //确认全选 1
    private void setAllItem(){
        for (int i = 0;i<list.size();i++){
            if (list.get(i).isFlag()==false){
                MyACAdapter.getMap().put(i,true);
                data.add(list.get(i).getName());
                ++checkNum;
            }
        }
        tag = false;
        if(mAdapter!=null)
        mAdapter.notifyDataSetChanged();
    }
    //取消全选 2
    private void setCancleAll(){
        for (int i = 0;i<list.size();i++){
            if (list.get(i).isFlag()==false){
                MyACAdapter.getMap().put(i,false);
                data.remove(list.get(i).getName());
                --checkNum;
            }
        }
        tag = true;
        if(mAdapter!=null)
        mAdapter.notifyDataSetChanged();
    }


    private void setAllItem2(){
        for (int i = 0;i<list.size();i++){
            MyACAdapter.getMap().put(i,true);
            data.add(list.get(i).getName());
        }
        checkNum=15;
        tag = false;
        if(mAdapter!=null)
        mAdapter.notifyDataSetChanged();
    }


    private void setCancleAll2(){
        for (int i = 0;i<list.size();i++){
            MyACAdapter.getMap().put(i,false);
            data.remove(list.get(i).getName());
        }
        checkNum = 15-list.size();
        tag = true;
        if(mAdapter!=null)
        mAdapter.notifyDataSetChanged();
    }


    //遍历数组;
    private String bianli(List<String> data){
        String result = null ;
        StringBuffer sb = new StringBuffer();
        if (data.size()==0){
            return null;
        }else{
            for (int i = 0;i< list.size();i++){
                for (int j = 0;j<data.size();j++){
                    if(list.get(i).getName().equals(data.get(j))){
                        sb.append(","+data.get(j));
                        break;
                    }
                }
            }
            result = sb.toString();
            result = result.replaceFirst(",","");
        }
        return result;
    }

    //增数据流程
    private void getNetAdd(String str){
        String url = UrlDealUtil.getRealUrl(RequestCode.ADD_PROCESS);
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("BookTitle",str);
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, this, CommonResultBean.class, RequestCode.ADD_PROCESS, paramsMap);
        HttpHandleUtil.executeCommonPost(this,httpRequestBean);
    }

    //减少数据流程
    private void cutBack(String str){
        String url = UrlDealUtil.getRealUrl(RequestCode.DELECT_PROCESS);
        HashMap<String,String> paramsMap=new HashMap<>();
        paramsMap.put("BookTitle",str);
        HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url, this, CommonResultBean.class, RequestCode.DELECT_PROCESS, paramsMap);
        HttpHandleUtil.executeCommonPost(this,httpRequestBean);
    }

    //返回键
    public void back(View view){
//        Intent intent = new Intent();
//        setResult(5,intent);
        finish();
    }


}
